<!DOCTYPE html>
<html lang="en" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container" >
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <div style="margin: 10px 10px 10px 10px">
                <div id="searchParam">
                    <div class="layui-form layui-form-pane" >
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <input type="text" id="roleName" class="layui-input" autocomplete="off" placeholder="请输入角色名称">
                            </div>

                            <div class="layui-inline">
                                <input type="text" size="50px" readonly class="layui-input" id="createTime" placeholder="创建时间">
                            </div>

                            <div class="layui-inline">
                                <select id="status">
                                    <option value="">请选择角色状态</option>
                                    <option value="1">正常</option>
                                    <option value="0">弃用</option>
                                </select>
                            </div>

                            <div class="layui-inline">
                                <button   onclick="search()"  class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <script type="text/html" id="toolbar">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="addNewRole" >添加</button>
                </div>
            </script>

            <table class="layui-hide" id="role_table" lay-filter="role_table"></table>

            <script type="text/html" id="tool">
                <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit" >编辑</a>
                <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="del" >删除</a>
            </script>
        </fieldset>
    </div>
</div>
</body>
<script src="/layui/layui.all.js"></script>
<script src="/js/core.util.js"></script>
<script>
    //获取token
    var token = CoreUtil.getData("access_token");
    //地址栏转义token中的#号
    var tokenQuery = token.replace("#", "%23");
    var tableIns1;
    var table = layui.table;
    var form = layui.form;
    var layer = layui.layer;
    var $ = jQuery = layui.jquery;
    var laydate = layui.laydate;
    var tree = layui.tree;
    var startTime = null;
    var endTime = null;
    layui.config({
        base: '/layui-ext/'
    }).extend({
        eleTree: 'eleTree/eleTree'
    }).use(['table', 'laypage', 'layer', 'laydate', 'tree', 'eleTree'], function () {
        tableIns1 = table.render({
            elem: '#role_table'
            , contentType: 'application/json'
            , headers: {"authorization": token}
            , page: true //开启分页
            , url: '/sys/roles' //数据接口
            , method: 'POST'
            , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": CoreUtil.isEmpty(res.data) ? 0 : res.data.total, //解析数据长度
                    "data": CoreUtil.isEmpty(res.data) ? null : res.data.records //解析数据列表
                }
            }
            , cols: [
                [
                    {type: 'checkbox', fixed: 'left'},
                    {field: 'name', title: '角色名称', width: 200},
                    {field: 'createTime', title: '创建时间', minWidth: 120},
                    {field: 'updateTime', title: '更新时间', minWidth: 120},
                    {
                        field: 'status', title: '状态', width: 100, templet: function (item) {
                            if (item.status === 1) {
                                return '  <input type="checkbox" lay-skin="switch" lay-text="正常|弃用" checked disabled>';
                            }
                            if (item.status === 0) {
                                return '  <input type="checkbox" lay-skin="switch" lay-text="正常|弃用" disabled>';
                            }
                        }
                    },
                    {field: 'description', title: '描述', width: 300},
                    {title: '操作', width: 250, toolbar: '#tool'}
                ]
            ]
            , toolbar: '#toolbar'
        });


        laydate.render({
            elem: '#createTime'
            , type: 'datetime'
            , range: '~'
            , done: function (value) {
                if (value != null && value != undefined && value != "") {
                    startTime = value.split("~")[0];
                    endTime = value.split("~")[1];
                } else {
                    startTime = "";
                    endTime = "";
                }

            }
        });
        table.on('toolbar(role_table)', function (obj) {
            switch (obj.event) {
                case 'addNewRole':
                    var index = layer.open({
                        title: '新增权限',
                        type: 2,
                        shade: 0.2,
                        maxmin:true,
                        shadeClose: true,
                        area: ['50%', '70%'],
                        content: 'roles/info',
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    break;
            }
        });
        table.on('tool(role_table)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'del':
                    tipDialog(data.id);
                    break;
                case 'edit':
                    var index = layer.open({
                        title: '修改权限',
                        type: 2,
                        shade: 0.2,
                        maxmin:true,
                        shadeClose: true,
                        area: ['50%', '70%'],
                        content: 'roles/info?id='+data.id,
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    break;
            }
        });


        var tipDialog = function (id) {
            layer.open({
                content: '确定要删除么',
                yes: function (index ) {
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                    CoreUtil.sendDelete("/sys/role/" + id, null, function (res) {
                        layer.msg(res.msg);
                        search();
                    });
                }
            });
        }
    });

    //执行查询
    function search() {
        //这里以搜索为例
        tableIns1.reload({
            where: { //设定异步数据接口的额外参数，任意设
                name: $("#roleName").val(),
                status: $("#status").val(),
                startTime: startTime,
                endTime: endTime,
            }
            , page: {
                curr: 1 //重新从第 1 页开始
            }
        });
    };
</script>
</html>